# Contributor: Díaz Urbaneja Diego <sodomon2@gmail.com>
# Maintainer: Díaz Urbaneja Diego <sodomon2@gmail.com>
pkgname=godot
pkgver=4.1.3
pkgrel=1
pkgdesc="Multi-platform 2D and 3D game engine"
url="https://godotengine.org"
license="MIT"
# Only armv7 and x86_64 are tested, give other architectures a try
# build system is completely broken and refuses to detect anything correctly
arch="x86_64 x86 armv7 aarch64"
makedepends="
	alsa-lib-dev
	enet-dev
	freetype-dev
	glslang-dev
	graphite2-dev
	harfbuzz-dev
	icu-dev
	libgudev-dev
	libogg-dev
	libpng-dev
	libtheora-dev
	libvorbis-dev
	libwebp-dev
	libx11-dev
	libxcursor-dev
	libxi-dev
	libxinerama-dev
	libxrandr-dev
	mbedtls2-dev
	mesa-dev
	miniupnpc-dev
	opusfile-dev
	pcre2-dev
	pulseaudio-dev
	python3-dev
	scons
	wslay-dev
	zlib-dev
	zstd-dev
	"
depends="ca-certificates-bundle"
subpackages="$pkgname-doc $pkgname-templates"
source="https://github.com/godotengine/godot/releases/download/$pkgver-stable/godot-$pkgver-stable.tar.xz
	gcc13.patch
	no-execinfo.patch
	"
options="!check" # No tests
builddir="$srcdir"/godot-"$pkgver"-stable
_sconsflags="
	builtin_certs=false
	builtin_enet=false
	builtin_freetype=false
	builtin_glslang=false
	builtin_graphite=false
	builtin_harfbuzz=false
	builtin_icu4c=false
	builtin_libogg=false
	builtin_libpng=false
	builtin_libtheora=false
	builtin_libvorbis=false
	builtin_libwebp=false
	builtin_mbedtls=false
	builtin_miniupnpc=false
	builtin_opus=false
	builtin_pcre2=false
	builtin_wslay=false
	builtin_zlib=false
	builtin_zstd=false
	execinfo=false
	system_certs_path=/etc/ssl/cert.pem
	use_sowrap=false
	speechd=false
	use_static_cpp=false
	"

case "$CARCH" in
aarch64|x86_64)
	makedepends="$makedepends embree3-dev"
	_sconsflags="$_sconsflags builtin_embree=false"
	;;
esac

case "$CARCH" in
x86_64)
	;;
*)
	# only builds on x86_64
	_sconsflags="$_sconsflags module_denoise_enabled=false"
	;;
esac

build() {
	case "$CARCH" in
	x86_64)
		local arch="x86_64"
		;;
	x86)
		local arch="x86_32"
		;;
	armv7)
		local arch="arm32"
		;;
	aarch64)
		local arch="arm64"
		;;
	esac
	export BUILD_NAME="alpine_linux"
	for target in editor template_release template_debug; do
		scons -j${JOBS:-1} \
			platform=linuxbsd \
			target=$target \
			arch=$arch \
			$_sconsflags \
			CFLAGS="$CFLAGS -O2 -flto=auto" \
			CXXFLAGS="$CXXFLAGS -O2 -flto=auto" \
			LINKFLAGS="$LDFLAGS"
	done
}

package() {
	pkgdesc="$pkgdesc (GUI editor)"
	install -Dm644 misc/dist/linux/org.godotengine.Godot.desktop \
		-t "$pkgdir"/usr/share/applications/
	install -Dm644 misc/dist/linux/org.godotengine.Godot.appdata.xml \
		-t "$pkgdir"/usr/share/metainfo/
	install -Dm644 icon.png "$pkgdir"/usr/share/icons/hicolor/256x256/apps/godot.png
	install -Dm644 icon.svg "$pkgdir"/usr/share/icons/hicolor/scalable/apps/godot.svg
	install -Dm644 misc/dist/linux/godot.6 -t "$pkgdir"/usr/share/man/man6/
	install -Dm755 bin/godot.*.editor.* "$pkgdir"/usr/bin/godot
	install -Dm755 bin/godot.*.template_debug.* "$pkgdir"/usr/bin/godot-template-debug
	install -Dm755 bin/godot.*.template_release.* "$pkgdir"/usr/bin/godot-template-release
}

templates() {
	pkgdesc="$pkgdesc (templates)"
	amove usr/bin/*template*
}

sha512sums="
e8dc812fc1bd98ce4d691d51fceb75d943fb40790c5a640e6178eb53b4de8b1078de8e1649e182963cb7beea26b2365c202f6deaea9bb679f789cd546d29f858  godot-4.1.3-stable.tar.xz
129cf8c0331190a111b8d397b7975ef2087486d3fb083b3e02612d2a07b2601d989842f2ec591e2300819d37c4070f74b8d50194f8f9af3219452b856d534ff8  gcc13.patch
7563942223564d5235d96a27c840ce0c9d7ceb9690ce1ed02f43c0c4e7b28e85f8311a18b79aeab545bdb83822604f05f98b561d2d00dd3a1b9cea60f528a7d8  no-execinfo.patch
"
